package com.weilele.leisure.time.ui.holder

import android.view.View
import com.weilele.leisure.time.R
import com.weilele.leisure.time.bean.PageByIdBean
import com.weilele.leisure.time.bean.VideoListBean
import com.weilele.leisure.time.databinding.HolderHomeBannerBinding
import com.weilele.leisure.time.ui.module.video_detail.VideoDetailFragment
import com.weilele.leisure.time.ui.module.video_detail.copyData
import com.weilele.library.banner.YzsBanner
import com.weilele.mvvm.adapter.MvvmHolder

class HomeBannerHolder : MvvmHolder<HomeBannerHolder.Bean, HolderHomeBannerBinding> {
    private val banner = YzsBanner<BannerBean>(mBinding.root)

    init {
        banner.setCanAutoScroll(true)
        banner.setListener(object : YzsBanner.Listener<BannerBean> {
            override fun onBannerClick(
                bannerItemView: View,
                bannerData: BannerBean,
                position: Int
            ) {
                super.onBannerClick(bannerItemView, bannerData, position)
                VideoDetailFragment.start(
                    bannerItemView,
                    VideoListBean.RowsDTO().apply { copyData(bannerData.data) })
            }
        })
    }

    constructor(binding: HolderHomeBannerBinding) : super(binding)
    constructor(itemView: View) : super(itemView)

    private class BannerBean(val data: PageByIdBean.ItemsDTO.CmsImgDTO) : YzsBanner.BannerData {
        override fun getBannerUrl(): Any {
            return data.picUrl
        }

        override fun getBannerTitle(): String? {
            return data.title
        }
    }

    data class Bean(var data: PageByIdBean)


    override fun bindData(data: Bean) {
        data.data.items
            ?.filter { it.cmsImg != null }
            ?.map { BannerBean(it.cmsImg) }
            ?.toMutableList()
            ?.let {
                banner.setDatas(it, R.layout.item_banner_img, true)
            }
    }
}